<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>网球场布局 - UniApp版本</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f5f5f5;
            padding: 10px;
            font-size: 14px;
        }

        .court-container {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            justify-content: center;
            align-items: center;
            min-height: 400px;
            background: white;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .tennis-court {
            width: 80px;
            height: 140px;
            position: relative;
            padding: 10px;
            background: #8BC34A;
            border-radius: 4px;
        }

        .court-lines {
            position: absolute;
            top: 10px;
            left: 10px;
            right: 10px;
            bottom: 10px;
            border: 1px solid white;
            background: #4CAF50;
            border-radius: 2px;
        }

        .net-line {
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: white;
            transform: translateY(-50%);
        }

        .service-line {
            position: absolute;
            top: 25%;
            left: 0;
            right: 0;
            height: 1px;
            background: white;
        }

        .service-line.bottom {
            top: 75%;
        }

        .center-line {
            position: absolute;
            top: 25%;
            left: 50%;
            bottom: 25%;
            width: 1px;
            background: white;
            transform: translateX(-50%);
        }

        /* 网格排列 */
        .court-container.grid {
            flex-direction: row;
            flex-wrap: wrap;
        }

        /* 水平排列 */
        .court-container.horizontal {
            flex-direction: row;
            flex-wrap: nowrap;
            overflow-x: auto;
        }

        /* 垂直排列 */
        .court-container.vertical {
            flex-direction: column;
            flex-wrap: nowrap;
        }

        /* 圆形排列 */
        .court-container.circular {
            position: relative;
            flex-wrap: nowrap;
        }

        .court-container.circular .tennis-court {
            position: absolute;
        }

        /* 响应式设计 */
        @media (max-width: 375px) {
            .tennis-court {
                width: 70px;
                height: 120px;
            }
            
            .court-lines {
                top: 8px;
                left: 8px;
                right: 8px;
                bottom: 8px;
            }
        }

        @media (max-width: 320px) {
            .tennis-court {
                width: 60px;
                height: 100px;
            }
            
            .court-lines {
                top: 6px;
                left: 6px;
                right: 6px;
                bottom: 6px;
            }
        }

        /* 俯瞰图效果 */
        .court-container.top-view {
            transform: perspective(600px) rotateX(45deg);
            transform-origin: center center;
        }

        .court-container.top-view .tennis-court {
            transform: translateZ(0);
        }

        /* 控制面板样式 */
        .controls {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .control-row {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .control-row:last-child {
            margin-bottom: 0;
        }

        .control-label {
            min-width: 80px;
            font-size: 14px;
            color: #333;
        }

        .control-input {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .color-input {
            width: 50px;
            height: 35px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .select-input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            background: white;
        }

        .view-toggle {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }

        .view-btn {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background: white;
            font-size: 14px;
            cursor: pointer;
            text-align: center;
        }

        .view-btn.active {
            background: #007AFF;
            color: white;
            border-color: #007AFF;
        }

        /* 信息面板 */
        .info-panel {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin-top: 15px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            font-size: 14px;
            color: #666;
        }

        .info-row {
            margin-bottom: 5px;
        }

        .info-row:last-child {
            margin-bottom: 0;
        }
    </style>
</head>
<body>
    <div class="controls">
        <div class="control-row">
            <span class="control-label">场内颜色:</span>
            <input type="color" class="color-input" id="courtColor" value="#4CAF50">
        </div>
        <div class="control-row">
            <span class="control-label">场外颜色:</span>
            <input type="color" class="color-input" id="outsideColor" value="#8BC34A">
        </div>
        <div class="control-row">
            <span class="control-label">球场数量:</span>
            <input type="number" class="control-input" id="courtCount" min="1" max="12" value="4">
        </div>
        <div class="control-row">
            <span class="control-label">排列方式:</span>
            <select class="select-input" id="arrangement">
                <option value="grid">网格排列</option>
                <option value="horizontal">水平排列</option>
                <option value="vertical">垂直排列</option>
                <option value="circular">圆形排列</option>
            </select>
        </div>
        <div class="view-toggle">
            <div class="view-btn active" data-view="normal">普通视图</div>
            <div class="view-btn" data-view="top">俯瞰图</div>
        </div>
    </div>

    <div class="court-container grid" id="courtContainer">
        <!-- 网球场将在这里动态生成 -->
    </div>

    <div class="info-panel">
        <div class="info-row">球场数量: <span id="courtCountDisplay">4</span></div>
        <div class="info-row">排列方式: <span id="arrangementDisplay">网格排列</span></div>
        <div class="info-row">视图模式: <span id="viewDisplay">普通视图</span></div>
    </div>

    <script>
        class UniAppTennisCourt {
            constructor() {
                this.courtContainer = document.getElementById('courtContainer');
                this.courtColorInput = document.getElementById('courtColor');
                this.outsideColorInput = document.getElementById('outsideColor');
                this.courtCountInput = document.getElementById('courtCount');
                this.arrangementSelect = document.getElementById('arrangement');
                this.viewButtons = document.querySelectorAll('.view-btn');
                
                this.courtCountDisplay = document.getElementById('courtCountDisplay');
                this.arrangementDisplay = document.getElementById('arrangementDisplay');
                this.viewDisplay = document.getElementById('viewDisplay');
                
                this.currentView = 'normal';
                
                this.init();
            }
            
            init() {
                this.createCourts();
                this.bindEvents();
            }
            
            bindEvents() {
                // 颜色变化事件
                this.courtColorInput.addEventListener('input', () => this.updateCourtColors());
                this.outsideColorInput.addEventListener('input', () => this.updateCourtColors());
                
                // 数量变化事件
                this.courtCountInput.addEventListener('input', () => this.updateCourtCount());
                
                // 排列方式变化事件
                this.arrangementSelect.addEventListener('change', () => this.updateArrangement());
                
                // 视图切换事件
                this.viewButtons.forEach(btn => {
                    btn.addEventListener('click', (e) => this.switchView(e.target.dataset.view));
                });
            }
            
            createCourts() {
                const count = parseInt(this.courtCountInput.value);
                this.courtContainer.innerHTML = '';
                
                for (let i = 0; i < count; i++) {
                    const court = this.createCourt(i);
                    this.courtContainer.appendChild(court);
                }
                
                this.updateArrangement();
                this.updateCourtColors();
            }
            
            createCourt(index) {
                const court = document.createElement('div');
                court.className = 'tennis-court';
                court.dataset.index = index;
                
                court.innerHTML = `
                    <div class="court-lines">
                        <div class="net-line"></div>
                        <div class="service-line"></div>
                        <div class="service-line bottom"></div>
                        <div class="center-line"></div>
                    </div>
                `;
                
                return court;
            }
            
            updateCourtColors() {
                const courtColor = this.courtColorInput.value;
                const outsideColor = this.outsideColorInput.value;
                const courts = this.courtContainer.querySelectorAll('.tennis-court');
                
                courts.forEach(court => {
                    court.style.backgroundColor = outsideColor;
                    const courtLines = court.querySelector('.court-lines');
                    courtLines.style.backgroundColor = courtColor;
                });
            }
            
            updateCourtCount() {
                const count = parseInt(this.courtCountInput.value);
                this.courtCountDisplay.textContent = count;
                this.createCourts();
            }
            
            updateArrangement() {
                const arrangement = this.arrangementSelect.value;
                const courts = this.courtContainer.querySelectorAll('.tennis-court');
                
                this.arrangementDisplay.textContent = this.arrangementSelect.options[this.arrangementSelect.selectedIndex].text;
                
                // 重置容器类名
                this.courtContainer.className = 'court-container';
                
                // 重置球场位置
                courts.forEach(court => {
                    court.style.position = 'relative';
                    court.style.left = 'auto';
                    court.style.top = 'auto';
                    court.style.margin = '4px';
                });
                
                switch (arrangement) {
                    case 'horizontal':
                        this.courtContainer.classList.add('horizontal');
                        break;
                    case 'vertical':
                        this.courtContainer.classList.add('vertical');
                        break;
                    case 'circular':
                        this.arrangeCircularly(courts);
                        this.courtContainer.classList.add('circular');
                        break;
                    default: // grid
                        this.courtContainer.classList.add('grid');
                }
            }
            
            arrangeCircularly(courts) {
                const containerRect = this.courtContainer.getBoundingClientRect();
                const centerX = containerRect.width / 2;
                const centerY = containerRect.height / 2;
                const radius = Math.min(centerX, centerY) - 60;
                
                courts.forEach((court, index) => {
                    const angle = (index / courts.length) * 2 * Math.PI;
                    const x = centerX + radius * Math.cos(angle) - 40; // 40 is half of court width
                    const y = centerY + radius * Math.sin(angle) - 70; // 70 is half of court height
                    
                    court.style.position = 'absolute';
                    court.style.left = x + 'px';
                    court.style.top = y + 'px';
                    court.style.margin = '0';
                });
            }
            
            switchView(view) {
                this.currentView = view;
                
                // 更新按钮状态
                this.viewButtons.forEach(btn => {
                    btn.classList.toggle('active', btn.dataset.view === view);
                });
                
                // 更新显示文本
                this.viewDisplay.textContent = view === 'top' ? '俯瞰图' : '普通视图';
                
                // 应用视图样式
                if (view === 'top') {
                    this.courtContainer.classList.add('top-view');
                } else {
                    this.courtContainer.classList.remove('top-view');
                }
            }
        }
        
        // 初始化应用
        document.addEventListener('DOMContentLoaded', () => {
            new UniAppTennisCourt();
        });
    </script>
</body>
</html>